using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using GeneCMS.Models.logical;
using BulletOnRailsControllers.Controllers;
using GeneCMS.Models.physical;
using Suteki.Common.Repositories;
using BulletOnRails4NET.Controllers;
using GeneCMS.Models.View;

namespace GeneCMS.Controllers
{
    [Authorize]
    public class WebsiteController : ScaffoldController<GeneCMS.Models.physical.Website>
    {
        //
        // GET: /Website/

        private BoardViewModel GetBoard()
        {
            Repository<Usr> user_data = new Repository<Usr>();
            var user = user_data.GetAll().Where(u => u.Username == User.Identity.Name).SingleOrDefault();
            //We assume that every user has a website and every website has at least one menu
            var board = new BoardViewModel() { Website = user.Website, Menu = user.Website.Menus[0] };
            return board;
        }
        private BoardViewModel UpdateBoard(Menu menu_edited)
        {
            Repository<Usr> user_data = new Repository<Usr>();
            var user = user_data.GetAll().Where(u => u.Username == User.Identity.Name).SingleOrDefault();
            //We assume that every user has a website and every website has at least one menu
            var board = new BoardViewModel() { Website = user.Website, Menu = user.Website.Menus[0] };
           // menu_edited.WebsiteID = board.Website.ID;
            board.Menu.AttributeToMatch = menu_edited.AttributeToMatch;
            board.Menu.ElementMatchCriteria = menu_edited.ElementMatchCriteria;
            board.Menu.EndLevel = menu_edited.EndLevel;
            board.Menu.StartLevel = menu_edited.StartLevel;
            user_data.SubmitChanges();
            //TODO: see if this works??
            return board;
        }
        private BoardViewModel UpdateBoard(GeneCMS.Models.physical.Website website_edited)
        {
            Repository<Usr> user_data = new Repository<Usr>();
            var user = user_data.GetAll().Where(u => u.Username == User.Identity.Name).SingleOrDefault();
            //We assume that every user has a website and every website has at least one menu
            var board = new BoardViewModel() { Website = user.Website, Menu = user.Website.Menus[0] };
           // menu_edited.WebsiteID = board.Website.ID;
            board.Website.TemplateFolder = website_edited.TemplateFolder;
            board.Website.TemplateMainFile = website_edited.TemplateMainFile;
            board.Website.ProductionFilePath = website_edited.ProductionFilePath;
            user_data.SubmitChanges();
            //TODO: see if this works??
            return board;
        }


        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Board()
        {
            return View(GetBoard());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Board(Menu menu_edited)
        {
            var board = UpdateBoard(menu_edited);
            
            return View(board);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Board(GeneCMS.Models.physical.Website website_edited)
        {
            var board = UpdateBoard(website_edited);
            
            return View(board);
        }

        public ActionResult MenuEdit()
        {
            return View();
        }

        public JsonResult GetPages()
        {
            WebsiteListViewModel view_model = new WebsiteListViewModel(GetUserWebsite().WebsiteID);
            return Json(
            
                view_model, JsonRequestBehavior.AllowGet)
            ;
        }


        /// <summary>
        /// Builds a logicalWebsite object that contains the system-wide
        /// preferences and correct links to editing and template folders.
        /// </summary>
        /// <param name="website_id"></param>
        /// <returns></returns>
        private GeneCMS.Models.logical.Website GetWebsite(int website_id)
        {
            Repository<GeneCMS.Models.physical.Website> website_data = new Repository<GeneCMS.Models.physical.Website>();
            var cms_website_data = website_data.GetById(website_id);

            GeneCMS.Models.logical.Website cms_website = new GeneCMS.Models.logical.Website()
            {
                AssetsBasePath =
                    HttpContext.Server.MapPath(PathResolver.GetSiteRoot() + "assets/" + cms_website_data.ID + "/"),
               
                PreviewBasePath =
                    HttpContext.Server.MapPath(PathResolver.GetSiteRoot() +  "preview/" + cms_website_data.ID + "/"),
                EditingBasePath =
                    HttpContext.Server.MapPath(PathResolver.GetSiteRoot() +  "editing/" + cms_website_data.ID + "/"),
                InjectCodeFilePath =
                HttpContext.Server.MapPath(PathResolver.GetSiteRoot() + "templates/EditCodeToInject.htm"),
                WebsiteTemplatePath =
                    HttpContext.Server.MapPath(PathResolver.GetSiteRoot() + "templates/" + cms_website_data.TemplateFolder + "/" + cms_website_data.TemplateMainFile),
                WebsiteTemplateFolder =
                HttpContext.Server.MapPath(PathResolver.GetSiteRoot() + "templates/" + cms_website_data.TemplateFolder + "/"),
                WebsiteID = cms_website_data.ID,
                WebsiteProductionPath = cms_website_data.ProductionFilePath
            };
            return cms_website;
        }

        public ActionResult EditPage(int id)
        {
            var cms_website = GetUserWebsite();
            cms_website.RenderWebsite();

            return Redirect(PathResolver.GetSiteRoot() + "editing/" + cms_website.WebsiteID + "/" + PageEdit.GetURLFromPageID(id));
        }

        private GeneCMS.Models.logical.Website GetUserWebsite()
        {
            Repository<Usr> user_data = new Repository<Usr>();
            var user = user_data.GetAll().Where(u => u.Username == User.Identity.Name).SingleOrDefault();
            return GetWebsite(user.WebsiteID.Value);
        }

        public ActionResult PublishWebsite()
        {

            var cms_website = GetUserWebsite();
            cms_website.PublishWebsite();

            return View(cms_website);
        }

        public ActionResult AssetsBrowse(string type, int levels_to_base)
        {
            AssetsBrowseViewModel model = new AssetsBrowseViewModel();
            var cms_website = GetUserWebsite();
            string assets_path = HttpContext.Server.MapPath(PathResolver.GetSiteRoot() + "assets/" + cms_website.WebsiteID + "/");
            
            model.UrlPrefixToBase = GeneCMS.Models.Helpers.UrlHelper.GetPrefixRelativeToBase(levels_to_base);
            model.GetAssets(model.UrlPrefixToBase, assets_path, assets_path, 0, 11);
            return View(model);
        }
        

    }
}
